package com.zjw.nanyinge.service.impl;

import com.zjw.nanyinge.feign.BaiduTokenApi;
import com.zjw.nanyinge.feign.BaiduVoiceApi;
import com.zjw.nanyinge.service.BaiduAiService;
import com.zjw.nanyinge.vo.req.BaiduTokenParamsVO;
import com.zjw.nanyinge.vo.req.BaiduVoiceParamsVO;
import com.zjw.nanyinge.vo.resp.BaiduTokenRespVO;
import com.zjw.nanyinge.vo.resp.VoiceVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class BaiduAiServiceImpl implements BaiduAiService {

    @Resource
    private BaiduTokenApi baiduTokenApi;
    @Resource
    private BaiduVoiceApi baiduVoiceApi;
    @Resource
    private CacheManager cacheManager;
    @Value("${baidu.apiKey}")
    private String apiKey;
    @Value("${baidu.secretKey}")
    private String secretKey;
    @Value("${baidu.grantType}")
    private String grantType;
    @Value("${baidu.api.voice.cuid}")
    private String cuid;
    @Value("${baidu.api.voice.ctp}")
    private String ctp;
    @Value("${baidu.api.voice.lan}")
    private String lan;
    @Value("${baidu.api.voice.spd}")
    private String spd;
    @Value("${baidu.api.voice.pit}")
    private String pit;
    @Value("${baidu.api.voice.vol}")
    private String vol;
    @Value("${baidu.api.voice.per}")
    private String per;
    @Value("${baidu.api.voice.aue}")
    private String aue;

    @Override
    public VoiceVO text2audio(String text) {
        //获取百度token
        BaiduTokenRespVO token = getToken();
        //文字转语音
        return text2audio(token, text);
    }

    private VoiceVO text2audio(BaiduTokenRespVO token, String text) {
        BaiduVoiceParamsVO baiduVoiceParamsVO = new BaiduVoiceParamsVO();
        baiduVoiceParamsVO.setTex(text);
        baiduVoiceParamsVO.setTok(token.getAccess_token());
        baiduVoiceParamsVO.setCuid(cuid);
        baiduVoiceParamsVO.setCtp(ctp);
        baiduVoiceParamsVO.setLan(lan);
        baiduVoiceParamsVO.setSpd(spd);
        baiduVoiceParamsVO.setPit(pit);
        baiduVoiceParamsVO.setVol(vol);
        baiduVoiceParamsVO.setPer(per);
        baiduVoiceParamsVO.setAue(aue);
        return baiduVoiceApi.text2audio(baiduVoiceParamsVO);
    }

    private BaiduTokenRespVO getToken() {
        Cache baiduVoiceCache = cacheManager.getCache("baiduVoice");
        BaiduTokenRespVO baiduToken = baiduVoiceCache.get("baiduToken", BaiduTokenRespVO.class);
        if (Objects.isNull(baiduToken)) {
            BaiduTokenParamsVO baiduTokenParamsVO = new BaiduTokenParamsVO();
            baiduTokenParamsVO.setClient_secret(secretKey);
            baiduTokenParamsVO.setClient_id(apiKey);
            baiduTokenParamsVO.setGrant_type(grantType);
            BaiduTokenRespVO baiduTokenRespVO = baiduTokenApi.oauthToken(baiduTokenParamsVO);
            baiduTokenRespVO.setExpiresDate(LocalDateTime.now().plusSeconds(baiduTokenRespVO.getExpires_in() - 60));
            baiduVoiceCache.put("baiduToken", baiduTokenRespVO);
            return baiduTokenRespVO;
        }
        LocalDateTime expiresDate = baiduToken.getExpiresDate();
        if (LocalDateTime.now().isAfter(expiresDate)) {
            baiduVoiceCache.evict("baiduToken");
            return getToken();
        }
        return baiduToken;
    }


}
